﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinkShareWpf.Commands;
using LinkShareWpf.Properties;
using System.Collections.ObjectModel;
using System.Windows;
using System.IO;

namespace LinkShareWpf.ViewModel
{
    public class OptionWindowViewModel
    {
  
        #region Commmands
        public DelegateCommand OkCommand { get; private set; }
        public DelegateCommand UndoCommand { get; private set; }
        #endregion

        #region Events
        public event EventHandler CloseWindowRequested;
        #endregion

        #region Private
        private string theme;
        #endregion

        #region Properties
        public int Port
        {
            get;
            set;
        }
        public string CoversPath
        {
            get;
            set;
        }
        public int TTL
        {
            get;
            set;
        }
        public int MaxNode
        {
            get;
            set;
        }
        public ObservableCollection<string> DictionaryList
        {
            get;
            set;
        }
        public string ThemeName
        {
            get;
            set;
        }
        #endregion

        public OptionWindowViewModel()
        {
            OkCommand = new DelegateCommand(o => Ok_Apply());
            UndoCommand = new DelegateCommand(o => Undo());
            Port = Settings.Default.PortNumber;
            // Questa proprietà indicherà il percorso della cartella contenente le covers all'avvio del programma
            CoversPath = Settings.Default.NewCoversPath;

            DictionaryList = new ObservableCollection<string>();
            DictionaryList.Add("Blue");
            DictionaryList.Add("Rosso");
            DictionaryList.Add("Grigio");
            DictionaryList.Add("Azzurro");

            theme = Settings.Default.Theme;
            ThemeName = FromSettingToName();

            TTL = Settings.Default.TTL;
            MaxNode = Settings.Default.MaxNode;
        }

        private void Undo()
        {
            CloseWindowRequested(this, EventArgs.Empty);
        }

        private void Ok_Apply()
        {
            // Il vecchio percorso delle cover viene salvato in una variabile temporanea
            string oldCoversPath = Settings.Default.CoversPath;

            theme = FromNameToSetting();
            Settings.Default.PortNumber = Port;
            Settings.Default.Theme = theme;
            Settings.Default.TTL = TTL;
            Settings.Default.MaxNode = MaxNode;
            
            // Se il vecchio percorso è diverso da quello nuovo
            if (!oldCoversPath.Equals(CoversPath))
                Settings.Default.NewCoversPath = CoversPath;
            Settings.Default.Save();

            Uri langDictUri = new Uri("ResourceDictionaries/" + theme, UriKind.Relative);
            ResourceDictionary langDict = Application.LoadComponent(langDictUri) as ResourceDictionary;
            Application.Current.Resources.MergedDictionaries.Clear();
            Application.Current.Resources.MergedDictionaries.Add(langDict);

            CloseWindowRequested(this, EventArgs.Empty);
        }

        // Dal setting di tipo string => al nome da visualizzare che è quello bindato
        string FromSettingToName()
        {
            string themeName = String.Empty;
            switch (theme)
            {
                case "ResourceDictionary1.xaml":
                    themeName = "Blue";
                    break;
                case "ResourceDictionary2.xaml":
                    themeName = "Rosso";
                    break;
                case "ResourceDictionary3.xaml":
                    themeName = "Grigio";
                    break;
                case "ResourceDictionary4.xaml":
                    themeName = "Azzurro";
                    break;
            }
           return themeName;
        }

        // Dal nome del tema alla stringa che finirà nel setting
        string FromNameToSetting()
        {
            string th = String.Empty;
            switch (ThemeName)
            {
                case "Blue":
                    th = "ResourceDictionary1.xaml";
                    break;
                case "Rosso":
                    th = "ResourceDictionary2.xaml";
                    break;
                case "Grigio":
                    th = "ResourceDictionary3.xaml";
                    break;
                case "Azzurro":
                    th = "ResourceDictionary4.xaml";
                    break;
            }
            return th;
        }
       
    }

}
